import { defineStore } from "pinia"
import { REC_TYPE, BANNER_TYPE } from '@utils/constant'
import { BannerSearch, Banner } from "@/models/banner"
import { RecCourse, RecCoursePageSearch, HotCourse, HotCourseSearch } from "@/models/course"
import { RecLive, RecLiveSearch } from "@/models/live"
import { RecMaterial, RecMaterialPageSearch, DocPkgInfo, DocPkgPageSearch } from "@/models/material"
import { Software } from "@/models/software"
import { bannerApi, courseApi, liveApi, materialApi, softwareApi } from "@/services"

const useSelectedContentStore  = defineStore('SelectedContent', {
    // state
    state:() =>({
        topBannerListResult: [] as Banner[],
        midBannerListResult: [] as Banner[],
        siderBannerListResult: [] as Banner[],
        liveListResult: [] as RecLive[],
        courseRecListResult: [] as RecCourse[],
        courseHotListResult: [] as HotCourse[],
        docPkgListResult: [] as DocPkgInfo[],
        materialListResult: [] as RecMaterial[],
        softwareListResult: [] as Software[],
    }),

    // actions
    actions:{
        // 初始化数据
        async initData(){
            await this.getTopBanners();
            await this.getMidBanners();
            await this.getSiderBanners();
            await this.getLives();
            await this.getRecCourses();
            await this.getHotCourses();
            await this.getRecDocPkgs();
            await this.getRecMatrials();
            await this.getRecSoftwares();
        },

        // 获得banner列表
        async getTopBanners() {
            try {
                const data = {
                    cate: BANNER_TYPE.USER_TOP,
                } as BannerSearch;
                const res = await bannerApi.getUserBanners(data)
                this.topBannerListResult = res? res : [] as Banner[]
            } catch(err) {
        
            }
        },

        // 获得banner列表
        async getMidBanners() {
            try {
                const data = {
                    cate: BANNER_TYPE.USER_MID,
                } as BannerSearch;
                const res = await bannerApi.getUserBanners(data)
                this.midBannerListResult = res? res : [] as Banner[]
            } catch(err) {
        
            }
        },

        // 获得banner列表
        async getSiderBanners() {
            try {
                const data = {
                    cate: BANNER_TYPE.USER_SIDER,
                } as BannerSearch;
                const res = await bannerApi.getUserBanners(data)
                this.siderBannerListResult = res? res : [] as Banner[]
            } catch(err) {
        
            }
        },

        // 获得直播录播列表
        async getLives() {
            try {
                const data = {
                    pageNum: 1,
                    pageSize: 7,
                } as RecLiveSearch;
                const res = await liveApi.getRecLives(data)
                console.log("getLives result: ", res)
                this.liveListResult = (res && res.code == '1') ? res.resultData : [] as RecLive[]
                console.log("getLives liveListResult = ", this.liveListResult)
            } catch(err) {
                
            }
        },

        // 获得推荐课程列表
        async getRecCourses() {
            try {
                const data = {
                    rec: REC_TYPE.SELECTED,
                    pageNum: 1,
                    pageSize: 6,
                } as RecCoursePageSearch;
                const res = await courseApi.getRecCourses(data);
                this.courseRecListResult = (res && res.records) ? res.records : [] as RecCourse[]
            } catch(err) {
                
            }
        },

        // 获得热门课程列表
        async getHotCourses() {
            try {
                const data = {
                    size: 5,
                } as HotCourseSearch;
                const res = await courseApi.getHotCourses(data, true);
                this.courseHotListResult = (res && res.courses) ? res.courses : [] as HotCourse[]
            } catch(err) {
                
            }
        },

        // 获取推荐资料包列表
        async getRecDocPkgs() {
            try {
                const data : DocPkgPageSearch = {
                    pageNum: 1,
                    pageSize: 5,
                };
                const res = await materialApi.getRecDocPkgs(data)
                this.docPkgListResult = (res && res.records) ? res.records : [] as DocPkgInfo[]
            } catch(err) {
        
            }
        },

        // 获取资料列表
        async getRecMatrials() {
            try {
                const data : RecMaterialPageSearch = {
                    rec: REC_TYPE.SELECTED,
                    pageNum: 1,
                    pageSize: 5,
                };
                const res = await materialApi.getRecMaterials(data)
                this.materialListResult = (res && res.records) ? res.records : [] as RecMaterial[]
            } catch(err) {
        
            }
        },

        // 获取软件列表
        async getRecSoftwares() {
            try {
                const res = await softwareApi.getRecSoftwares(5)
                this.softwareListResult = res? res : [] as Software[]
            } catch(err) {
        
            }
        },

        // 加为我的资料
        async addAsMyMaterial(id:number) {
            try {
                const res = await materialApi.addAsMyMaterial(id)
                return res? res.result : false
            } catch(err) {
                return false
            }
        },
    },
    getters:{
        topBannerList:(state) => state.topBannerListResult,
        midBannerList:(state) => state.midBannerListResult,
        siderBannerList:(state) => state.siderBannerListResult,
        liveSource:(state) => state.liveListResult,
        courseRecSource:(state) => state.courseRecListResult,
        courseHotSource:(state) => state.courseHotListResult,
        materialSource:(state) => state.materialListResult,
        softwareSource:(state) => state.softwareListResult,
    }
})

export default useSelectedContentStore;